<?php
/**
 * 展位
 * Created by PhpStorm.
 * User: Apache
 * Date: 2019/10/28
 * Time: 23:05
 */
namespace app\api\controller\activemanage;
use app\api\controller\common\ApiBase;
use Think\Db;
use think\Controller;
class Boothscale extends Controller
{
    protected $admin;
    protected $model = null;
    public function _initialize()
    {
        parent::_initialize();
        $this->admin = session('admin');
        $this->model = model('Booth');
    }

    public function getTabName($exhibition_id){
        $result=db('exhibition')->where(['id'=>$exhibition_id])->find();
        $tabName="";
        if($result){
            $tabName=$result['create_tablename'];
        }else{
            return json(['code'=>0,'msg'=>'缺少关键字段','data'=>'']);
        }
        return $tabName;
     }

    //查询展会所在的区域
    public function boothAreaList(){
        if (request()->isPost()) {
            $post = input('post.');
            $exhibition_id = isset($post['exhibition_id']) && $post['exhibition_id'] != '' ? $post['exhibition_id'] : 0;
            if(!$exhibition_id){
                return json(array('code' => '0', 'msg' => '展会信息错误'));
            }
            $list=db('booth')->Distinct(true)->field('area_code')->where(['exhibition_id'=>$exhibition_id,'area_code'=>['<>','not null'],'is_delete'=>0])->order('area_code asc')->select();
            foreach ($list as &$vv) {
                $vv['total'] = db('booth')->where(['area_code' => $vv['area_code'],'exhibition_id'=>$exhibition_id,'is_delete'=>0])->count();
            }
            $total=db('booth')->where(['exhibition_id'=>$exhibition_id,'is_delete'=>0])->count();
            return json(array('code' => '1', 'msg' => '操作成功','data'=>$list,'total'=>$total));
        } else {
            return json(array('code' => '0', 'msg' => '请求失败'));
        }
    }


    //查询具体区域数据
    public function boothSaleList(){
        if (request()->isPost()) {
            $post = input('post.');
            $exhibition_id = isset($post['exhibition_id']) && $post['exhibition_id'] != '' ? $post['exhibition_id'] : 0;
            $area_code = isset($post['area_code']) && $post['area_code'] != '' ? $post['area_code'] : '';
            if(!$exhibition_id){
                return json(array('code' => '0', 'msg' => '展会信息错误'));
            }
            $param=" and m.is_delete=0 and m.exhibition_id=$exhibition_id ";
            if($area_code){
                $param.=" and m.area_code='$area_code'";
            }
            // 1已付款 2，未付款 3，可出售
            $tabName=$this->getTabName($exhibition_id);
            $sql2="select m.*,c.company_name as agent_info,c.status as company_status";
            $sql2.=" from crm_booth m ";
            $sql2.=" left join crm_".$tabName." as c on c.id=m.agent_id";
            $sql2.=" where 1=1 $param";
            $sql2.=" order by m.id desc ";
//            var_dump($sql2);die();
            $list=Db::query($sql2);
            $infos=array(
                array('status_name'=>'已付款','status'=>1,'booth_child'=>array()),
                array('status_name'=>'未付款','status'=>2,'booth_child'=>array()),
                array('status_name'=>'可出售','status'=>3,'booth_child'=>array())
            );
            $child1=array();$child2=array();$child3=array();
            if(!empty($list)){
                foreach ($list as &$v){
                    if(!$v['agent_id'] || $v['agent_id']==null || empty($v['agent_id'])){
                        $child3[]=$v;
                    }else{
                        if($v['company_status']>4){
                            $child1[]=$v;
                        }else{
                            $child2[]=$v;
                        }
                    }
                }
                unset($v);
                $infos[0]['booth_child']=$child1;
                $infos[1]['booth_child']=$child2;
                $infos[2]['booth_child']=$child3;
            }
            return json(['code'=>1,'msg'=>'操作成功','data'=>$infos]);
        } else {
            return json(array('code' => '0', 'msg' => '请求失败'));
        }
    }

}